package com.hzit.controller;

import com.hzit.model.LayuiPage;
import com.hzit.model.Response;
import com.hzit.model.User;
import com.hzit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/getAll")
    public List<User> queryAll(){

        List<User> list = userService.list();
        log.info("*******查询结果：" + list);
        return list;

    }

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @PostMapping(value="/user/insert")
    public Response insert(@RequestBody User user){

        boolean save = userService.save(user);
        log.info("*******插入结果："+user);

        if(save){
            return new Response(200,"插入数据成功",user);
        }else {
            return new Response(500,"插入数据失败",null);
        }
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @GetMapping(value = "/user/get/{id}")
    public Response selectById(@PathVariable("id")Long id){

        User user = userService.selectById(id);

        log.info("*******查询结果："+user);

        if(user != null){
            return new Response(200,"查询数据成功",id);
        }else {
            return new Response(500,"没有数据记录，查询数据失败"+id,null);
        }
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping(value = "/user/delete/{id}")
    public Response delete(@PathVariable("id")Long id){

        User user = new User();

        boolean delete = userService.removeById(id);

        log.info("*******删除的结果："+delete);

        if(delete){
            return new Response(200,"删除数据成功",user);
        }else {
            return new Response(500,"没有数据记录，删除数据失败",null);
        }
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping(value="/user/update")
    public Response update(User user){

        boolean update = userService.updateById(user);

        log.info("*******删除的结果："+update);

        if(update){
            return new Response(200,"修改数据成功",user);
        }else {
            return new Response(500,"没有数据记录，修改数据失败",null);
        }
    }


    @PutMapping(value="/user/update2")
    public Response update2(@RequestBody Long id){

        //先查询数据
        User user = userService.selectById(id);
        //在修改数据
        boolean update = userService.updateById(user);

        if(update){
            return new Response(200,"修改数据成功",user);
        }else {
            return new Response(500,"没有数据记录，修改数据失败",null);
        }
    }

/*
    @GetMapping("/user/page")
    public Response page() {

        Page<User> page = new Page<>(1,3) ;


        if (user != null) {
            return new Response(200, "查询数据成功", user);
        } else {
            return new Response(500, "没有数据记录，查询数据失败", null);
        }
    }*/
}
